package com.example.fresh_market.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.Request;
import com.example.fresh_market.adapter.OrderAdmAdapter;
import com.example.fresh_market.databinding.ActivityOrderManageBinding;
import com.example.fresh_market.request.AdmOrderPageResp;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.request.DeliverOrderReq;
import com.example.fresh_market.request.HandleOrderReq;
import com.example.fresh_market.request.PageResult;
import com.example.fresh_market.utils.HttpUtil;
import com.google.gson.Gson;

import cn.hutool.core.lang.TypeReference;

public class OrderManageActivity extends AppCompatActivity {

    private ActivityOrderManageBinding binding;
    private Context mContext;
    private OrderAdmAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityOrderManageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = this;
        Gson gson = new Gson();
        RecyclerView orderRecycler = binding.recyclerOrderAdmin;
        orderRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new OrderAdmAdapter(new OrderAdmAdapter.OnOrderClickListener() {
            @Override
            public void onDeliverClick(View view, AdmOrderPageResp order) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("发货提示");
                builder.setMessage("你确定要发货吗？");
                builder.setCancelable(false);
                builder.setPositiveButton("确定", (dialog, which) -> {
                    DeliverOrderReq orderReq = DeliverOrderReq.builder().orderId(order.getId()).build();
                    HttpUtil.sendJsonRequest(mContext, Request.Method.POST, "/admin/order/deliver"
                            , gson.toJson(orderReq), response -> {
                                ApiResponse apiResponse = gson.fromJson(response.toString(), ApiResponse.class);
                                if (apiResponse.getCode() == 200) {
                                    dialog.dismiss();
                                    initData();
                                }
                            });
                });
                builder.setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                });
                AlertDialog deliverDialog = builder.create();
                deliverDialog.show();
            }

            @Override
            public void onHandleRefundClick(View view, AdmOrderPageResp order) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("退款处理提示");
                builder.setMessage("是否同意退款？");
                builder.setCancelable(false);
                builder.setPositiveButton("同意", (dialog, which) -> {
                    handleRefundRequest(dialog, order, true);
                });
                builder.setNegativeButton("拒绝", (dialog, which) -> {
                    handleRefundRequest(dialog, order, false);
                });
                builder.setNeutralButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                });
                AlertDialog deliverDialog = builder.create();
                deliverDialog.show();
            }
        });
        orderRecycler.setAdapter(adapter);
        initData();
    }

    private void handleRefundRequest(DialogInterface dialog, AdmOrderPageResp order, boolean isAgree) {
        HandleOrderReq orderReq = HandleOrderReq.builder()
                .orderId(order.getId())
                .isAgree(isAgree)
                .build();
        Gson gson = new Gson();
        HttpUtil.sendJsonRequest(mContext, Request.Method.POST, "/admin/order"
                , gson.toJson(orderReq), response -> {
                    ApiResponse apiResponse = gson.fromJson(response.toString(), ApiResponse.class);
                    if (apiResponse.getCode() == 200) {
                        dialog.dismiss();
                        initData();
                    }
                });
    }

    public void initData() {
        HttpUtil.get(mContext, "/admin/order?page=1&size=10000", response -> {
            ApiResponse<PageResult<AdmOrderPageResp>> apiResponse = new Gson().fromJson(response, new TypeReference<ApiResponse<PageResult<AdmOrderPageResp>>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                adapter.submitList(apiResponse.getData().getList());
            }
        });
    }
}
